# -*- coding: utf-8 -*-
"""
临时入口点文件
用于uvicorn热重载
"""
import logging
import os
import sys
from pathlib import Path

# 将项目目录添加到路径
project_root = Path(__file__).resolve().parent
if str(project_root) not in sys.path:
    sys.path.append(str(project_root))

from core.log import logger, InterceptHandler
from utils.logger import Log
from core.server import InitializeApp

# 初始化日志
def init_logger():
    """初始化日志配置"""
    # 配置uvicorn日志使用loguru
    LOGGER_NAMES = ("uvicorn", "uvicorn.access", "uvicorn.error", "fastapi")
    logging.getLogger().handlers = [InterceptHandler()]
    for logger_name in LOGGER_NAMES:
        logging_logger = logging.getLogger(logger_name)
        logging_logger.handlers = [InterceptHandler()]
        # 调整Uvicorn日志级别，隐藏一些不必要的消息
        if logger_name in ("uvicorn", "uvicorn.access"):
            logging_logger.setLevel(logging.WARNING)
    
    # 尝试配置loguru (如果尚未配置)
    try:
        # 仅当未配置时进行配置
        if len(logger._core.handlers) <= 1:
            Log.configure_logger()
        logger.info("日志系统初始化完成")
    except Exception as e:
        print(f"日志系统初始化失败: {str(e)}")

# 初始化日志
init_logger()

# 创建应用实例
app = InitializeApp()

# 打印启动信息
display_ip = "127.0.0.1"  # 默认值
try:
    import socket
    hostname = socket.gethostname()
    display_ip = socket.gethostbyname(hostname)
except:
    pass

logger.success(f"应用入口点已加载，可访问: http://{display_ip}:10161/docs")
